//
//  BaseNavigationController.m
//  Insurance
//
//  Created by Hanten on 2017/4/25.
//  Copyright © 2017年 baoduobang. All rights reserved.
//

#import "BaseNavigationController.h"

@interface BaseNavigationController ()<UIGestureRecognizerDelegate, UINavigationControllerDelegate>

@property (nonatomic, weak) UIViewController *currentShowVC;

@end

@implementation BaseNavigationController

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    
    BaseNavigationController *baseVC = [super initWithRootViewController:rootViewController];
    self.interactivePopGestureRecognizer.delegate = self;
    baseVC.delegate = self;
    return baseVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self setNavigationTheme];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self setNavigationTheme];
//    [HSTool setStatusBarBackgroundColor:kMainColor];
}

- (void)setNavigationTheme {

    UINavigationBar *navBar = [UINavigationBar appearance];
    // 设置导航栏背景
    [navBar setBackgroundImage:[HSTool createImageWithColor:kMainColor andHeight:1] forBarMetrics:UIBarMetricsDefault];
    [navBar setBarTintColor:kMainColorA(0)];
    [navBar setTintColor:[UIColor whiteColor]];
    // 设置状态栏背景
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    // 设置导航栏的文字
    [navBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],
                                     NSFontAttributeName: [UIFont boldSystemFontOfSize:18]}];
    [navBar setShadowImage:[UIImage new]];
    
    [self.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kTabbarNormalColor} forState:UIControlStateNormal];
    [self.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kTabbarSelectColor} forState:UIControlStateSelected];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (navigationController.viewControllers.count == 1) {
        self.currentShowVC = nil;
    }else {
        self.currentShowVC = viewController;
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    
    if (gestureRecognizer == self.interactivePopGestureRecognizer) {
        return (self.currentShowVC == self.topViewController);
    }
    return YES;
}


- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.navigationBarHidden = NO;
    
    if (self.viewControllers.count > 0) {
        
        viewController.hidesBottomBarWhenPushed = YES;
        UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"public_backArrow"] style:UIBarButtonItemStyleDone target:self action:@selector(popController)];
        viewController.navigationItem.leftBarButtonItem = back;
    }
    
    [super pushViewController:viewController animated:animated];
    
}


- (void)popController {
    [self popViewControllerAnimated:YES];
}


@end
